﻿/**

 * Copyright (c) 2015-2016, FastDev 刘强 (fastdev@163.com) & Quincy.

 *

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *      http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */


using OF.DB.DataAccess.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using OF.DB.DataAccess;
using OF.DB.Test.Entity;

namespace OF.DB.Test
{
    public class TestQueryGenerateClass
    {
        public static void Test()
        {
            QueryBlogs(new QueryBlogRequest());
            QueryBlogs(new QueryBlogRequest { id = 1 });
            QueryBlogs(new QueryBlogRequest { authorname = "n1" });
            QueryBlogs(new QueryBlogRequest { title = "t", authorname = "n" });
            QueryBlogs(new QueryBlogRequest { title = "t1", authorname = "n" });
            QueryBlogs(new QueryBlogRequest { title = "t1", authorname = "n1" });
            QueryBlogs(new QueryBlogRequest { authoruid = 1 });
            QueryBlogs(new QueryBlogRequest { authoruid = 1, id = 1 });
            Console.ReadLine();
        }
        public static void QueryBlogs(QueryBlogRequest request)
        {
            string sqlFormat = "SELECT b.* FROM dbo.Blog b WITH(NOLOCK) {0} {1}";
            Dictionary<int, string> aliasDict = new Dictionary<int, string>();
            Dictionary<int, string> joinTableDict = new Dictionary<int, string>();
            aliasDict.Add((int)BlogQueryTableEnum.Blog, "b");
            aliasDict.Add((int)BlogQueryTableEnum.User, "u");
            joinTableDict.Add((int)BlogQueryTableEnum.User, "INNER JOIN dbo.[User] u WITH(NOLOCK) ON b.authoruid=u.Id");
            SqlAndConditionBuilder builder = new SqlAndConditionBuilder(aliasDict, joinTableDict);
            if (request.id != null)
            {
                builder.AddEqual(BlogQueryTableEnum.Blog, "id", SqlDbType.Int, request.id.Value);
            }
            if (!string.IsNullOrEmpty(request.title))
            {
                builder.AddContains(BlogQueryTableEnum.Blog, "title", SqlDbType.VarChar, request.title);
            }
            if (request.authoruid != null)
            {
                builder.AddEqual(BlogQueryTableEnum.Blog, "authoruid", SqlDbType.Int, request.authoruid.Value);
            }
            if (!string.IsNullOrEmpty(request.authorname))
            {
                builder.AddContains(BlogQueryTableEnum.User, "name", SqlDbType.VarChar, request.authorname);
            }
            SqlQueryContext context = builder.Build(sqlFormat);
            int count = DBHelper.Count(context);
            Console.WriteLine("sql is:" + context.Sql);
            Console.WriteLine("result count:" + count);
            List<Blog> blogList = DBHelper.GetList<Blog>(context, "Id DESC", 1, 2);
            Console.WriteLine("result page 1:" + Program.GetJsonString(blogList));

            blogList = DBHelper.GetList<Blog>(context, "Id DESC", 2, 2);
            Console.WriteLine("result page 2 :" + Program.GetJsonString(blogList));
            Console.WriteLine("\n\n");
        }

        private enum BlogQueryTableEnum : int
        { 
            Blog = 1,
            User = 2
        }
    }

    public class QueryBlogRequest
    {
        public int? id;
        public string title;
        public int? authoruid;
        public string authorname;
    }
}
